// component/product/index.js

const {
    default: Toast
} = require('../../vant/weapp/toast/toast');
const app = getApp()
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        item: {
            type: Object,
            value: [],
            observer(e) {
                if (wx.getStorageSync('items')) {
                    this.setData({
                        items: wx.getStorageSync('items'),
                        product: e.sku.sku_product,
                        detail: e
                    });
                    this.getids()
                } else {
                    for (let i = 0; i < e.sku.spec.length; i++) {
                        let n = e.sku.spec[i].list
                        for (let m = 0; m < n.length; m++) {
                            n[m].isChecked = false
                        }
                    }
                    this.setData({
                        items: e.sku.spec,
                        product: e.sku.sku_product,
                        detail: e
                    });
                }
                console.log(this.data.items)
            },
        },
        store: {
            type: String,
            value: "",
            observer(e) {
                this.setData({
                    store_id: e
                })
            }
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        active: 0,
        num: 1,
        items: [], //规格
        product: [], //多规格详情
        productDetail: [], //所选规格详情
        detail: [], //商品详情
        sku: '',
        ids: "",
        store_id: '',
        is_user_show: true,
        is_user: 0,
        showUser: false,
    },

    computed: {


    },
    /**
     * 组件的方法列表
     */
    methods: {
        //   登录
        login() {
            if (wx.getStorageSync('token') == "") {
                this.setData({
                    is_user_show: false,
                    is_user: 1
                });
            } else if (wx.getStorageSync('mobile') == "") {
                this.setData({
                    is_user_show: false,
                    is_user: 2
                });
            }
        },
        // 登录遮罩层
        close(e) {
            this.setData({
                is_user_show: e.detail,
                is_user: 0
            });
        },
        // 登陆后更新
        renew(e) {
            this.setData({
                showUser: true,
            });
            wx.setStorageSync('userInfo', e.detail)
            wx.setStorageSync('mobile', e.detail.mobile)
        },
        buy() {
            const that = this
            if (wx.getStorageSync('mobile') == "") {
                that.login()
                return
            }
            if (!this.data.productDetail.id) {
                Toast.fail("请选择规格")
                return
            }
            app.http.post(app.api.IntegralMakeOrder, {
                store_id: this.data.store_id,
                address_id: wx.getStorageSync('address').id,
                goods_id: this.data.detail.id,
                spec: this.data.ids,
                sku_product: this.data.productDetail.id,
                num: this.data.num
            }, (res) => {
                wx.navigateTo({
                    url: '/pages/integral/exchange/index?order_on=' + res.data.order_on,
                    // url: '/pages/integral/exchange/index?order_on=' + "202301141135077132764832",
                })
            })

        },
        //获取商品不同规格的详情
        getids() {
            let ids = [];
            let sku = [];
            //遍历，已选规格，根据规格，查找详情
            for (let i = 0; i < this.data.items.length; i++) {
                let n = this.data.items[i].list
                for (let m = 0; m < n.length; m++) {
                    if (n[m].isChecked) {
                        ids.push(n[m].id);
                        sku.push(n[m].title)
                    }
                }
            }
            ids = ids.toString().replace(/,/g, '-')
            console.log(sku.toString())
            if (!this.data.product[ids]) {
                return
            }
            this.setData({
                ids,
                productDetail: this.data.product[ids],
                sku,
            })
        },

        //选择商品规格
        chooseSpec(e) {
            let index = e.currentTarget.dataset.index //第几种规格
            let i = e.currentTarget.dataset.i //第几个规格
            let check = e.currentTarget.dataset.checked //是否选中
            let n = this.data.items[index].list //第几种规格的列表
            let ii = 0
            for (let m = 0; m < n.length; m++) { //选择规格前，将第几种规格的全部规格选择改为false,一种规格只能选一个      
                n[m].isChecked = false
            }
            let newlist = this.data.items; //小程序的限制只能整体赋值，单独提出来更改后在赋值
            newlist[index].list[i].isChecked = !check;
            console.log("chooseSpec", newlist[index].list[i].title)
            this.getids()
            this.setData({
                items: newlist,
            })
        },

        //关闭
        close() {
            wx.setStorageSync('items', this.data.items)
            let sku = [];
            let i = 0
            for (let i = 0; i < this.data.items.length; i++) {
                let n = this.data.items[i].list
                for (let m = 0; m < n.length; m++) {
                    if (!n[m].isChecked) {
                        i++
                    }
                }
                if(i==n.length){
                    this.data.sku=0
                }
            }

            console.log('items', this.data.sku, this.data.num, this.data.items)
            this.triggerEvent("is_product", {
                is_product: false,
                sku: this.data.sku,
                num: this.data.num,
                ids:this.data.ids,
                sku_product:this.data.productDetail.id
            });
        },
        mins() {
            if (this.data.num <= 1) {
                return
            }
            let num = this.data.num - 1
            this.setData({
                num
            })
        },
        add() {
            let num = this.data.num + 1
            this.setData({
                num
            })
        },
    }
})